# -*- coding: utf-8 -*-
"""
Module Description: 日志记录通用类
Date: 2017/7/24
Author:Sky
"""
from pyshare.framework.logproxy.log_proxy import GameLogProxy
from pyshare.framework.logproxy.non_core_log_proxy import NonCoreLogProxy
from pyshare.framework.error_handle import GameException
from pyshare.const.lang.tips_info import TipsInfo


class LogManager(object):
    """日志管理记录类"""
    @staticmethod
    def log_game(log_param):
        """统一记录元宝和资源日志"""
        if isinstance(log_param.scene, int):
            log_param.scene = str(log_param.scene)

        free_gold_log_field_name = 'free_gold'
        pay_gold_log_field_name = 'pay_gold'
        if log_param.field_name in [free_gold_log_field_name, pay_gold_log_field_name]:
            LogManager.log_game_coin(log_param)
        else:
            LogManager.log_game_vary(log_param)

    @staticmethod
    def log_game_coin(log_param):
        """元宝日志"""
        GameLogProxy.coin(role_id=log_param.rid,
                          field_name=log_param.field_name,
                          old_val=log_param.old_val,
                          new_val=log_param.new_val,
                          scene=log_param.scene,
                          remark=log_param.remark,
                          reserve=log_param.reserve)

    @staticmethod
    def log_game_vary(log_param):
        """资源日志"""
        NonCoreLogProxy.vary(role_id=log_param.rid,
                             field_name=log_param.field_name,
                             old_val=log_param.old_val,
                             new_val=log_param.new_val,
                             scene=log_param.scene,
                             remark=log_param.remark,
                             reserve=log_param.reserve,
                             server_id=getattr(log_param, 'server_id', None))

    @staticmethod
    def log_game_event(log_param):
        """事件日志"""
        event_code = str(log_param.event_code)
        NonCoreLogProxy.event(role_id=log_param.rid,
                              system_id=event_code.split('-')[0],
                              subsystem_id=event_code.split('-')[1],
                              event_id=event_code.split('-')[2],
                              event_data=log_param.event_data,
                              remark=log_param.remark,
                              reserve=log_param.reserve)

    @staticmethod
    def log_game_rank(log_param):
        """排行榜日志"""
        if hasattr(log_param, "rid"):
            raise GameException(TipsInfo.GlobalKeys.paramError)

        event_code = str(log_param.event_code)
        NonCoreLogProxy.rank_log(system_id=event_code.split('-')[0],
                                 subsystem_id=event_code.split('-')[1],
                                 event_id=event_code.split('-')[2],
                                 rank_data=log_param.rank_data,
                                 remark=log_param.remark,
                                 reserve=log_param.reserve)

    @staticmethod
    def log_role_property(log_param):
        """角色属性日志"""
        NonCoreLogProxy.role_property(role_id=log_param.rid,
                                      property_id=log_param.property_id,
                                      cur_val=log_param.cur_val,
                                      reserve=log_param.reserve)

    @staticmethod
    def stop():
        """资源释放"""
        GameLogProxy.stop()
